package com.example.android.snake;

import java.util.ArrayList;
import java.util.Random;

import android.widget.TextView;

public class Graph {
	int size,csize;
	public ArrayList<PairInt> dotXY = new ArrayList<PairInt>();
	public ArrayList<Integer> dotState = new ArrayList<Integer>();
	// 1= ordinary
	// 2= near the clique;
	// 3= in the clique;
	public ArrayList<PairInt> edgeList = new  ArrayList<PairInt>();
	public int map[][];
	public static final int eps = 100;
    private static final Random RNG = new Random();
    public boolean isEmpty; 
    public int nowcsize = 0;
    public void updateStatus() {
    	for (int i = 0; i < size; i++){
    		int cnt = 0;
    		for (int j = 0 ; j < size; j++)
    			if (map[i][j]==1 && dotState.get(j).intValue()==3) 
    				 cnt ++;
    		
    	    if (dotState.get(i).intValue()!=3)
    	    	dotState.set(i,Integer.valueOf(cnt==nowcsize?2:1));
    	}
    }
	public Graph(int width, int height, int _size, int _csize,double scale) {
	  this.size = _size;
	  this.csize = _csize;
	  map = new int[_size][_size];
	  dotXY.clear(); dotState.clear();
	  for (int i = 0; i<size;i++) {
		  dotXY.add(new PairInt(
				   RNG.nextInt((int)(width*scale))+(int)(width*(1-scale)*0.5),
				   RNG.nextInt((int)(height*scale))+(int) (height*(1-scale)*0.5)));
		  dotState.add(Integer.valueOf(1));
	  }
	  edgeList.clear();
	  for (int i = 0 ; i < size; i++)
		  for (int j = 0 ; j < size; j++)
			    map[i][j] = 0;
	  for (int i = 0;i<csize;i++)
		  for (int j = i+1; j<csize; j++) {
			    edgeList.add(new PairInt(i,j));
	            map[i][j]=1;map[j][i] = 1;
		  }
	  for (int cnt = 0; cnt<(size*size-csize*csize)*0.3; cnt++) {
	     int tx = 0 , ty = 0;
	     while ((tx<csize && ty <csize) ||tx==ty)  {
	    	 tx = RNG.nextInt(size); ty = RNG.nextInt(size);
	     }
	     edgeList.add(new PairInt(tx,ty));
	     map[tx][ty]=1;map[ty][tx]=1;
	  }
	  this.isEmpty = false;
	  updateStatus();
	}
	public Graph(int width, int height) {
		this(width,height,10,5,0.95);
	}
	public Graph() {
		this.isEmpty = true;
	}
	public PairInt getNearestDot(int x0, int y0) {
	   int tmp = 1 << 30;
	   int id = 0;
	   for (int i = 0; i < size; i++) {
		   int px = dotXY.get(i).x; 
		   int py = dotXY.get(i).y;
		   int now = (x0-px)*(x0-px)+(y0-py)*(y0-py);
		   if (now<tmp) {
			   tmp = now; id = i;
		   }
	   }
	   
	   return new PairInt(tmp,id);
	}

	public int getNearestByXY(int x0, int y0) {   
	   int id = -1;
	   int tmp = (int) eps;
	   for (int i = 0; i < size; i++) {
		   int px = dotXY.get(i).x; 
		   int py = dotXY.get(i).y;
		   int now = (x0-px)*(x0-px)+(y0-py)*(y0-py);
		   if (now<tmp) {
			   tmp = now; id = i;
		   }
	   }
	   return id;
	}
	public void setXY(int selDot, float x, float y) {
		dotXY.set(selDot, new PairInt((int)x,(int)y));
	}
	public void reverseStateByXY(int x0, int y0) {
		PairInt c = getNearestDot(x0,y0);
		
	    if (c.x<eps) {
	    	if (dotState.get(c.y).intValue()==2) {
	    		dotState.set(c.y, Integer.valueOf(3));
	    		nowcsize++;
	    	} else 
	    if (dotState.get(c.y).intValue()==3) {
    		dotState.set(c.y, Integer.valueOf(2));
	        nowcsize--;
	    }
	    

	    updateStatus();
	    }
		
	}
}
